/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.JeuListener;
import Modele.Case;
import Modele.Dictionnaire;
import Modele.Grille;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Stack;
import java.util.TreeSet;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Gloire
 */public class Essai extends JPanel{
    
     private Rectangle[][] polygon;
     private LabelLettre[][] tabCase = new LabelLettre[4][4];
     private Grille grille;
     private Case lastCase;
     
     private JeuListener listener;
     
     private Dictionnaire dico;
     
     private StringBuffer encours;
     private Stack<LabelLettre> mot;
     private int points = 0;
     
     private TreeSet<String> solutions = new TreeSet();
     private Stack<String> trouves;
     private int total = 0;
     
     
     private Score s;
     
     private JLabel score;
     private JLabel temps;
     private int tempsInt;
     private boolean fin;
     
     private int xp = 200;
     private int yp = 10;
     
     private AudioClip son;
     
     public Stack<String> getTrouves(){
         return trouves;
     }
     public Grille getGrille(){
         return grille;
     }
     
     public Dictionnaire getDico(){
         return dico;
     }
     
     public void setSolutions(TreeSet<String> sol){
         solutions = new TreeSet();
         for(String str : sol){
             solutions.add("<tr><th align = LEFT>"+str+"</th></tr>");
         }
     }
     
     public TreeSet<String> getSolutions(){
         return solutions;
     }
     
     public int getTemps(){
         return tempsInt;
     }
     
     public void setScore(Score sframe){
         s = sframe;
     }
     
     public Essai(JLabel chrono, int time,Fenetre frame){
         temps = chrono;
         tempsInt = time;
         mot = new Stack();
         trouves = new Stack();
         encours = new StringBuffer();
         lastCase = null;
         grille = new Grille();
         dico = new Dictionnaire(grille.getGrille());
         BorderLayout g = new BorderLayout();
         setLayout(g);
         generePolygon();
         listener = new JeuListener(this);
         addMouseListener(listener);
	 addMouseMotionListener(listener);
         setPreferredSize(new Dimension(700, 505));
         
         //add("West",s);
         chrono();
     }
     
     private void chrono() {
		new java.util.Timer().schedule(new java.util.TimerTask() {
			@Override
			public void run() {
				if (--tempsInt > 0 && !fin) {
					temps.setText("Chrono : " + tempsInt+"     Score : "+total);
					chrono();
				}
			}
		}, 1000);
                if(tempsInt == 1){
                    fini();
                    this.removeMouseListener(listener);
                    this.removeMouseMotionListener(listener);
                }
	}
     
     @Override
     public void paintComponent(Graphics g){
         super.paintComponent(g);
         //Plateau de jeu
         g.drawRect(xp, yp,500,505);
         g.drawImage(Fenetre.createIcon("fond bleu.png", "grille").getImage(), 0, 0,null);
         
            int x = xp;
            int y = yp;
            int h = 115;
         
            for(int i = 0; i < 4; i++){
                for(int j = 0; j < 4; j++){
                    tabCase[j][i].paint(g);
                    //Espace vertical entre les carres
                    y += 125;
                }             
                 //Espace horizontal entre les carres
                y = yp;
                x += 125;
            } 
         
         repaint();
     }
     
     public void fini(){
         fin = true;
         temps.setText("Ton Score : "+total+" - tu as trouve "+trouves.size()+" mots sur "+solutions.size());
         resetLabels();
         repaint();
     }
     
     public final void generePolygon(){
         polygon = new Rectangle[4][4];
         
         int x = xp;
         int y = yp;
         int h = 115;
         
         for(int i = 0; i < 4; i++){
             for(int j = 0; j < 4; j++){
                 LabelLettre l = new LabelLettre(x,y,h,grille.getCase(i, j).getValeur(),grille.getCase(i, j).getPoints()); 
                 tabCase[j][i] = l;
                 polygon[i][j] = new Rectangle(x,y,h,h);
                 y += 125;
             }
             y = yp;
             x += 125;
         }
     }
     
     public void select(Point point){
         
         for(int i = 0; i < 4; i++){
             for(int j = 0; j < 4 ;j++){
                 if(polygon[i][j].contains(point) && !grille.getCase(i,j).estVisitee() && ((lastCase != null && grille.getCase(i,j).estVoisin(lastCase)) || lastCase == null)){
                     encours.append(grille.getCase(i, j).getValeur());
                     if(encours.length() < 5)
                        points += grille.getCase(i, j).getPoints();
                     else
                        points += grille.getCase(i, j).getPoints()+5;
                     mot.push(tabCase[j][i]);
                     grille.getCase(i, j).setVisitee(true);
                     lastCase = grille.getCase(i, j);
                     tabCase[j][i].setColor(1);
                     repaint();
                     break;
                 }
             }
         }
     }
     
     public void relache(){ 
         String str = encours.toString();        
         if(dico.getDico().contains(str) && !trouves.contains("<tr><th align = LEFT>"+str+"</th><th>"+points+"</th></tr>")){
             total += points;
             trouves.add("<tr><th align = LEFT>"+str+"</th><th>"+points+"</th></tr>");
             s.setFind();
             for(LabelLettre l : mot)
                 l.setColor(2);
             jouerSonExcellent();
         }
         else{
             for(LabelLettre l : mot)
                 l.setColor(-1);
             jouerSonMauvais();
         }
         for(int i = 0; i < 4; i++){
             for(int j = 0; j < 4 ;j++){
                     grille.getCase(i, j).setVisitee(false);
                     break;
             }
         }
         encours = new StringBuffer();
         lastCase = null;
         points = 0;
         repaint();
     }
     
     public void resetLabels(){
         for(int i = 0; i < 4; i++){
             for(int j = 0; j < 4 ;j++){
                 grille.getCase(i, j).setVisitee(false);
                 tabCase[j][i].setColor(0);
             }
         }
         mot.clear();
     }

   public void jouerSonExcellent(){
        
        java.net.URL url = Fenetre.class.getResource("son/chime2.wav");
        son = Applet.newAudioClip(url);
        son.play();
    }
     
       public void jouerSonMauvais(){
        
        java.net.URL url = Fenetre.class.getResource("son/bad.wav");
        son = Applet.newAudioClip(url);
        son.play();
    }
 
 }
